import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { ConfigService } from './config.service';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class TagService {
  private _map: any = {};
  private cache = false;
  constructor(private http: Http, private config: ConfigService) {
  }
  public get(tagCode: string, primeng?: boolean): Observable<any> {
    if (this._map[tagCode] && this.cache) {
      return Observable.fromPromise(Promise.resolve(this._map[tagCode]));
    }
    const url = this.config.get('url') + '?service=TagService&method=getTagInfo';
    return this.http.post(url, { tagCode: tagCode }).map((resp: Response) => {
      const result = resp.json();
      if (result.code === '0000') {
        const r = [];
        result.info.forEach((a: any) => {
          r.push(a);
        });
        if (this.cache) {
          this._map[tagCode] = r;
        }

        return r;
      }
    });
  }
  
}
